#include <Windows.h>
#include <tchar.h>
#include <WindowsX.h>
#include "resource.h"

int CALLBACK DlgProc(HWND hDlg, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
	TCHAR szTmp[128];
	if (nMsg >= WM_MOUSEFIRST && nMsg <= WM_MOUSELAST)
	{
		_stprintf(szTmp, _T("Mouse event 0x%0X"), nMsg);
		ListBox_AddString(GetDlgItem(hDlg, IDC_LIST1), szTmp);
		SendMessage(GetDlgItem(hDlg, IDC_LIST1), WM_VSCROLL, SB_BOTTOM, 0);
	}
	 
	switch (nMsg)
	{
		case WM_INITDIALOG:
		{
			SetWindowText(hDlg, _T("Demo0099"));

			UINT nWidth		= GetSystemMetrics(SM_CXSCREEN);
			UINT nHeight	= GetSystemMetrics(SM_CYSCREEN);
			RECT rtClient;
			GetClientRect(hDlg, &rtClient);
			SetWindowPos(hDlg, NULL, (nWidth - (rtClient.right - rtClient.left)) / 2, (nHeight - (rtClient.bottom - rtClient.top)) / 2, 0, 0, SWP_NOSIZE);
			
			break;
		}
		case WM_LBUTTONDOWN:
		{
			SetDlgItemText(hDlg, IDC_STATIC1, _T("D"));
			break;
		}
		case WM_MOUSEMOVE:
		{
			_stprintf(szTmp, _T("x: %d  y: %d %d"), GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), GetDoubleClickTime());
			SetDlgItemText(hDlg, IDC_STATIC6, szTmp);
			break;
		}
		case WM_LBUTTONUP:
		{
			SetDlgItemText(hDlg, IDC_STATIC1, _T("U"));
			break;
		}
		case WM_MBUTTONDOWN:
		{
			SetDlgItemText(hDlg, IDC_STATIC2, _T("D"));
			break;
		}
		case WM_MBUTTONUP:
		{
			SetDlgItemText(hDlg, IDC_STATIC2, _T("U"));
			break;
		}
		case WM_RBUTTONDOWN:
		{
			SetDlgItemText(hDlg, IDC_STATIC3, _T("D"));
			break;
		}
		case WM_RBUTTONUP:
		{
			SetDlgItemText(hDlg, IDC_STATIC3, _T("U"));
			break;
		}
		case WM_CLOSE:
		{
			PostQuitMessage(0);
			break;
		}
		case WM_LBUTTONDBLCLK:
		{
			SetDlgItemText(hDlg, IDC_STATIC1, _T("LDB"));
			break;
		}
		case WM_RBUTTONDBLCLK:
		{
			SetDlgItemText(hDlg, IDC_STATIC3, _T("RDB"));
			break;
		}
		case WM_MOUSEWHEEL:
		{
			TCHAR szTmp[MAX_PATH];
			_stprintf(szTmp, _T("%s"), GET_WHEEL_DELTA_WPARAM(wParam) > 0 ? _T("SU") : _T("SD"));
			SetDlgItemText(hDlg, IDC_STATIC2, szTmp);
			break;
		}
	}

	return 0;
}

BOOL WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPTSTR szCmdLine, int nShowCmd)
{
	DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG), NULL, DlgProc);

	return TRUE;
}